<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="关键词" prop="keyword">
        <el-input v-model="queryParams.keyword" placeholder="请输入搜索关键词" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="目录类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable>
          <el-option label="资源适用" :value="0" />
          <el-option label="产品适用" :value="1" />
          <el-option label="资源和产品都适用" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="FieldTypeList" @selection-change="handleSelectionChange" row-key="id" :tree-props="{children: 'childrenList', hasChildren: 'hasChildren'}">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="目录体系名" align="center" prop="catalogName" />
      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
          <span v-if="scope.row.type === 0">资源适用</span>
          <span v-else-if="scope.row.type === 1">产品适用</span>
          <span v-else-if="scope.row.type === 2">资源和产品都适用</span>
        </template>
      </el-table-column>
      <el-table-column label="体系描述" align="center" prop="remark" />
      <el-table-column label="用户级别" align="center" prop="userLevel" />
      <el-table-column label="最大关键词数量" align="center" prop="maxKeywordsNumber" />

      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <!-- -1冻结，0正常 -->
          <span v-if="scope.row.status === 0">正常</span>
          <span v-else-if="scope.row.status === -1">冻结</span>
          <span v-else>未知状态</span>
        </template>
      </el-table-column>

      <el-table-column label="创建用户" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="体系名称" prop="catalogName">
          <el-input v-model="form.catalogName" placeholder="请输入体系名称" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="资源适用" :value="0" />
            <el-option label="产品适用"  :value="1"/>
            <el-option label="资源和产品都适用"  :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="体系描述" prop="remark">
          <el-input type='textarea' v-model="form.remark" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="用户级别" prop="userLevel">
          <el-input v-model="form.userLevel" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="最大关键字数量" prop="maxKeywordsNumber">
          <el-input v-model="form.maxKeywordsNumber" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCatalog,
  getCatalog,
  addCatalog,
  updateCatalog
} from "@/api/qysj/app/SystemCatalog.js";

export default {
  name: "FieldType",
  data() {
    return {
      options: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      fieldNames:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引擎信息信息表格数据
      FieldTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //对话框宽度
      formLabelWidth:'80px',
      // 对话框表单设置
      settingForm:{
        userLevel:[],
        userType:[],
        specificUser:[]
      },
      // 模拟用户类型，用户级别，指定用户数据
      userLevels: [
        { label: '初级', value: 'primary' },
        { label: '中级', value: 'middle' },
        { label: '高级', value: 'senior' },
        // Add more options as needed
      ],
      userTypes: [
        { label: '个人', value: 'type1' },
        { label: '企业', value: 'type2' },
        { label: '政府人员', value: 'type3' },
        // Add more options as needed
      ],
      specifiedUsers: [
        { label: '张三', value: 'user1' },
        { label: '李四', value: 'user2' },
        { label: '王麻子', value: 'user3' },
        { label: 'zcl', value: 'user4' },
        { label: '张欣悦', value: 'user5' },
        // Add more options as needed
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        maxKeywordsNumber: [
          { required: true, message: "最大关键字数量不能为空", trigger: "blur" },
        ],
        catalogName: [
          { required: true, message: "体系名称不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" },
        ],
        userLevel: [
          { required: true, message: "用户级别不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询引擎信息信息列表 */
    getList() {
      this.loading = true;
      listCatalog(this.queryParams).then((response) => {
        this.FieldTypeList = response.rows;
        this.total = response.total;
        function bl(arr) {
          let newArr = [];
          arr.forEach((obj) => {
            let newa = {};
            newa.label = obj.fieldName;
            newa.value = obj.id;
            newa.parentId = obj.parentId;
            if (!obj.childrenList || obj.childrenList.length == 0) {
              newa.children = null;
              newArr.push(newa);
            } else {
              newa.children = bl(obj.childrenList||[]);
              newArr.push(newa);
            }

          });
          return newArr;
        }
        this.options = bl(response.rows||[]);
        // console.log(this.options)
        this.loading = false;
      });
    },
    handleChange(val){
    this.form.parentId = val[val.length-1];
		console.log('parentId>>>>>',this.form.parentId)
	  },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.fieldNames = selection.map((item) => item.fieldName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加目录分类信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCatalog(id).then((response) => {
        this.form = response.data;

        this.open = true;
        this.title = "修改分类信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let parames = JSON.parse(JSON.stringify(this.form));
          if(parames.keyWords && parames.keyWords.length>0){
            parames.keyWords.replace(/，/g,",")//中英文逗号
            parames.keywordList = parames.keyWords.split(",");
          }
          if (this.form.id != null) {
            if (!parames.parentId) { parames.parentId = 0; parames.parentName = ""};
            updateCatalog(parames).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (!parames.parentId) { parames.parentId = 0; parames.parentName = ""};
            addCatalog(parames).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  },
};
</script>
